package org.jeecg.modules.demo.summary.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.poi.hpsf.Decimal;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.decoration.entity.DecorationDeposit;
import org.jeecg.modules.demo.decoration.service.IDecorationDepositService;
import org.jeecg.modules.demo.electriccharge.entity.ElectricCharge;
import org.jeecg.modules.demo.electriccharge.entity.OtherElectricCharge;
import org.jeecg.modules.demo.electriccharge.mapper.ElectricChargeMapper;
import org.jeecg.modules.demo.electriccharge.mapper.OtherElectricChargeMapper;
import org.jeecg.modules.demo.electriccharge.service.IElectricChargeService;
import org.jeecg.modules.demo.flowable.model.ProcessStatusEntity;
import org.jeecg.modules.demo.flowable.service.IProcessMethods;
import org.jeecg.modules.demo.monthlyitem.entity.MonthlySummaryItem;
import org.jeecg.modules.demo.monthlyitem.service.IMonthlySummaryItemService;
import org.jeecg.modules.demo.prerecharge.entity.OtherPreRecharge;
import org.jeecg.modules.demo.prerecharge.entity.PreRecharge;
import org.jeecg.modules.demo.prerecharge.mapper.OtherPreRechargeMapper;
import org.jeecg.modules.demo.prerecharge.service.IPreRechargeService;
import org.jeecg.modules.demo.report1.entity.GjshReport;
import org.jeecg.modules.demo.report1.entity.Yingshou;
import org.jeecg.modules.demo.report1.mapper.GjshYingshouMapper;
import org.jeecg.modules.demo.shenhe.entity.HywyReportProcessApproveRecord;
import org.jeecg.modules.demo.shenhe.service.IHywyReportProcessApproveRecordService;
import org.jeecg.modules.demo.summary.entity.MonthlySummary;
import org.jeecg.modules.demo.summary.mapper.MonthlySummaryMapper;
import org.jeecg.modules.demo.summary.service.IMonthlySummaryService;
import org.jeecg.modules.system.entity.*;
import org.jeecg.modules.system.model.DepartIdModel;
import org.jeecg.modules.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: 月度汇总报表
 * @Author: jeecg-boot
 * @Date:   2024-04-02
 * @Version: V1.0
 */
@Service
public class MonthlySummaryServiceImpl extends ServiceImpl<MonthlySummaryMapper, MonthlySummary> implements IMonthlySummaryService {
    @Autowired
    private IDecorationDepositService decorationDepositService;
    @Autowired
    private IMonthlySummaryItemService monthlySummaryItemService;
    @Autowired
    private ISysDepartService departService;
    @Autowired
    private IElectricChargeService electricChargeService;
    @Autowired
    private ISysCategoryService sysCategoryService;
    @Autowired
    private OtherElectricChargeMapper otherElectricChargeMapper;
    @Autowired
    private OtherPreRechargeMapper otherPreRechargeMapper;
    @Autowired
    private IPreRechargeService preRechargeService;
    //管家生活物业费应收Mapper
    @Autowired
    private GjshYingshouMapper gjshYingshouMapper;
    //用户角色表
    @Autowired
    private ISysUserRoleService sysUserRoleService;
    //角色 服务
    @Autowired
    private ISysRoleService sysRoleService;

    //注入流程服务
    @Autowired
    private IProcessMethods processMethods;

    /**
     * 获取当前实体类
     * @param monthlySummary
     * @return
     */
    @Override
    public MonthlySummary getMonthlySummary(MonthlySummary monthlySummary){
        QueryWrapper<MonthlySummary> monthlySummaryQueryWrapper = new QueryWrapper<>();
        monthlySummaryQueryWrapper.eq("id",monthlySummary.getId());
        List<MonthlySummary> summaryList = list(monthlySummaryQueryWrapper);
        MonthlySummary monthlySummaryItem = summaryList.get(0);
        //更新审核结果
        monthlySummaryItem.setPass(monthlySummary.getPass());
        //更新备注
        monthlySummaryItem.setRemark(monthlySummary.getRemark());
        //返回
        return monthlySummaryItem;
    }
    @Override
    public Result<String> shenheTable(MonthlySummary monthlySummary,LoginUser user){
        //根据用户ID得到角色code
        SysRole roleOfUser = getRoleOfUser(user.getId());
        String roleCode = roleOfUser.getRoleCode();
        if (StrUtil.isEmpty(roleCode)){
            return Result.OK("当前用户没有职务！");
        }
        if (StrUtil.equals(roleCode,"wuyezuanyuan")){
            //物业专员的处理逻辑
            //获取该用户部门的物业经理的ID值
            String quyuId = selectLocalWyjlId(user.getId());
            if (StrUtil.isEmpty(quyuId)){
                return Result.OK("当前部门没有物业经理");
            }
            String pass = monthlySummary.getPass();
            boolean value;
            if ("Y".equals(pass)) {
                value = true;
                //完成此任务 并传下个任务的审核人
                processMethods.completeSomeoneTask(user.getId(),monthlySummary.getActProcessId(), ProcessStatusEntity.quyujingli,quyuId);
                //更新报表
                //修改报表状态
                monthlySummary.setProgress("1");
                //更新pass
                monthlySummary.setPass(monthlySummary.getPass());
                //设置备注
                monthlySummary.setRemark(monthlySummary.getRemark());
                updateById(monthlySummary);
                //添加审核记录
                hywyReportProcessApproveRecordService.addShenheJilu2(user,monthlySummary,roleOfUser);
                return Result.OK("物业专员审核成功");
            } else {
                value = false;
                //取消任务
                processMethods.cancelProcess(monthlySummary.getActProcessId());
                //修改报表状态
                monthlySummary.setProgress("0");
                //更新pass
                monthlySummary.setPass(monthlySummary.getPass());
                //设置备注
                monthlySummary.setRemark(monthlySummary.getRemark());
                updateById(monthlySummary);
                //添加审核记录
                hywyReportProcessApproveRecordService.addShenheJilu4(user,monthlySummary,roleOfUser);
                return Result.OK("物业专员已取消审核");
            }
        }else if(StrUtil.equals(roleCode,"wuyejingli")) {
            //物业经理的处理逻辑
            //获取该用户部门的区域经理的ID值
            String quyuId = selectLocalQujlId(user.getId());
            if (StrUtil.isEmpty(quyuId)) {
                return Result.OK("当前部门没有区域经理");
            }
            //完成此任务 并传下个任务的审核人
            String pass = monthlySummary.getPass();
            boolean value;
            if ("Y".equals(pass)) {
                value = true;
                //修改报表状态
                monthlySummary.setProgress("2");
            } else {
                value = false;
                //修改报表状态
                monthlySummary.setProgress("7");
            }
            processMethods.completeSomeoneTask(user.getId(), monthlySummary.getActProcessId(), ProcessStatusEntity.quyujingli, quyuId, value);
            //更新报表
            updateById(monthlySummary);
            //添加审核记录
            hywyReportProcessApproveRecordService.addShenheJilu2(user,monthlySummary,roleOfUser);
            return Result.OK("审核成功");
        }else if (StrUtil.equals(roleCode,"quyujingli")){
            //区域的处理逻辑
            //获取该用户部门物业总经理的ID值
            String quyuId = selectLocalWuyezongjingliId(user.getId());
            if (StrUtil.isEmpty(quyuId)){
                return Result.OK("当前部门没有集团财务");
            }
            //完成此任务 并传下个任务的审核人
            String pass = monthlySummary.getPass();
            boolean value ;
            if (pass.equals("Y")){
                value = true;
                //修改报表状态
                monthlySummary.setProgress("3");
            }else {
                value = false;
                //修改报表状态
                monthlySummary.setProgress("7");
            }
            processMethods.completeSomeoneTask(user.getId(),monthlySummary.getActProcessId(),ProcessStatusEntity.wuyezongjingli,quyuId,value);
            //更新报表
            updateById(monthlySummary);
            //添加审核记录
            hywyReportProcessApproveRecordService.addShenheJilu2(user,monthlySummary,roleOfUser);
            return Result.OK("审核成功");
        }else if (StrUtil.equals(roleCode,"wuyezongjingli")){
            //物业总经理的处理逻辑
            //获取该用户部门的集团财务的ID值
            String quyuId = selectLocalJituancaiwuId(user.getId());
            if (StrUtil.isEmpty(quyuId)){
                return Result.OK("当前部门没有集团财务");
            }
            //完成此任务 并传下个任务的审核人
            String pass = monthlySummary.getPass();
            boolean value ;
            if (pass.equals("Y")){
                value = true;
                //修改报表状态
                monthlySummary.setProgress("4");
            }else {
                value = false;
                //修改报表状态
                monthlySummary.setProgress("7");
            }
            processMethods.completeSomeoneTask(user.getId(),monthlySummary.getActProcessId(),ProcessStatusEntity.jituancaiwu,quyuId,value);
            //更新报表
            updateById(monthlySummary);
            //添加审核记录
            hywyReportProcessApproveRecordService.addShenheJilu2(user,monthlySummary,roleOfUser);
            return Result.OK("审核成功");
        } else if (StrUtil.equals(roleCode,"jituancaiwu")){
            //集团财务的处理逻辑
            //获取该用户部门的集团会计的ID值
            String quyuId = selectLocalJituankuaijiId(user.getId());
            if (StrUtil.isEmpty(quyuId)){
                return Result.OK("当前部门没有集团财务");
            }
            //完成此任务 并传下个任务的审核人
            String pass = monthlySummary.getPass();
            boolean value ;
            if (pass.equals("Y")){
                value = true;
                //修改报表状态
                monthlySummary.setProgress("5");
            }else {
                value = false;
                //修改报表状态
                monthlySummary.setProgress("7");
            }
            processMethods.completeSomeoneTask(user.getId(),monthlySummary.getActProcessId(),ProcessStatusEntity.jituankuaiji,quyuId,value);
            //更新报表
            updateById(monthlySummary);
            //添加审核记录
            hywyReportProcessApproveRecordService.addShenheJilu2(user,monthlySummary,roleOfUser);
            return Result.OK("审核成功");
        }else if(StrUtil.equals(roleCode,"jituankuaiji")){
            //集团财务的处理逻辑
            //如果为集团会计 则直接进行通过
            processMethods.completeSomeoneTask001(user.getId(),monthlySummary.getActProcessId());
            //修改报表状态
            monthlySummary.setProgress("6");
            //更新报表
            updateById(monthlySummary);
            //添加审核记录
            hywyReportProcessApproveRecordService.addShenheJilu3(user,monthlySummary,roleOfUser);
            return Result.OK("推送完成！");
        }
        return Result.error("当前用户无权限");
    }

    /**
     * 根据用户ID得出该用户的角色对象
     * @param userId
     */
    public SysRole getRoleOfUser(String userId){
        //根据用户ID查找角色ID
        QueryWrapper<SysUserRole> sysUserRoleQueryWrapper = new QueryWrapper<>();
        sysUserRoleQueryWrapper.eq("user_id",userId);
        //得到该用户的角色  假定用户只有一个角色对象
        List<SysUserRole> sysUserRoleList = sysUserRoleService.list(sysUserRoleQueryWrapper);
        QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();
        sysRoleQueryWrapper.eq("id",sysUserRoleList.get(0).getRoleId());
        //根据角色对象的ID找出此角色的编码
        List<SysRole> sysRoleList = sysRoleService.list(sysRoleQueryWrapper);
        return sysRoleList.get(0);
    }
    /**
     *  查找当前小区下的物业经理ID
     * @param userId
     * @return
     */
    @Override
    public String selectLocalWyjlId(String userId) {
        //根据用户ID找到部门ID
        List<DepartIdModel> departIdModels = userDepartService.queryDepartIdsOfUser(userId);
        //这是此用户的部门ID
        String departId = departIdModels.get(0).getKey();
        //根据角色编码 获取到系统角色对象
        SysRole wuyejingli = sysRoleService.getRoleNoTenant("wuyejingli");
        //获取到系统角色的id值
        String wuyejingliId = wuyejingli.getId();
        //根据系统角色ID在用户和角色表中找到所有用户的角色是该角色的ID
        QueryWrapper<SysUserRole> sysUserRoleQueryWrapper = new QueryWrapper<>();
        sysUserRoleQueryWrapper.eq("role_id",wuyejingliId);
        //得到所有角色是物业经理的集合 里面包含用户ID
        List<SysUserRole> list = sysUserRoleService.list(sysUserRoleQueryWrapper);
        //再根据用户ID筛选是本部门的
        for (SysUserRole sysUserRole : list) {
            //根据用户的ID遍历每个用户 得到每个用户的部门对象 再根据部门对象找到部门ID 找到和登录用户相同的部门ID
            List<DepartIdModel> departIdModels1 = userDepartService.queryDepartIdsOfUser(sysUserRole.getUserId());
            if (departIdModels1.get(0).getKey().equals(departId)){
                //如果相等 就返回此用户iD
                return sysUserRole.getUserId();
            }
        }
        return null;
    }
    /**\
     * 查找当前小区的区域经理ID
     * @param userId
     * @return
     */
    @Override
    public String selectLocalQujlId(String userId) {
        //根据用户ID找到部门ID
        List<DepartIdModel> departIdModels = userDepartService.queryDepartIdsOfUser(userId);
        //这是此用户的部门ID
        String departId = departIdModels.get(0).getKey();
        //根据部门ID找到部门编码
        QueryWrapper<SysDepart> departQueryWrapper = new QueryWrapper<>();
        departQueryWrapper.eq("id",departId);
        List<SysDepart> departList = departService.list(departQueryWrapper);
        //部门编码
        String orgCode = departList.get(0).getOrgCode();
        //取部门编码前六位
        if (StrUtil.isEmpty(orgCode)){
            return null;
        }
        //区域编码
        String substring = orgCode.substring(0, 6);
        //根据区域编码找到区域的部门ID
        QueryWrapper<SysDepart> departQueryWrapper2 = new QueryWrapper<>();
        departQueryWrapper2.eq("org_code",substring);
        //找到区域部门的对象
        List<SysDepart> departList1 = departService.list(departQueryWrapper2);
        //得到区域部门的ID值
        String id = departList1.get(0).getId();
        //根据ID值筛选角色列表
        //根据角色编码 获取到系统角色对象
        SysRole wuyejingli = sysRoleService.getRoleNoTenant("quyujingli");
        //获取到系统角色的id值
        String wuyejingliId = wuyejingli.getId();
        //根据系统角色ID在用户和角色表中找到所有用户的角色是该角色的ID
        QueryWrapper<SysUserRole> sysUserRoleQueryWrapper = new QueryWrapper<>();
        sysUserRoleQueryWrapper.eq("role_id",wuyejingliId);
        //得到用户和系统角色集合都是区域经理的集合 里面包含用户ID 和角色ID
        List<SysUserRole> list = sysUserRoleService.list(sysUserRoleQueryWrapper);
        //遍历此集合找到每个用户
        for (SysUserRole sysUserRole : list) {
            //根据用户ID找到部门ID
            List<DepartIdModel> departIdModels2 = userDepartService.queryDepartIdsOfUser(sysUserRole.getUserId());
            //当集合中的用户的 部门ID和登录用户的部门ID相等时，则返回集合用户的ID值
            if (departIdModels2.get(0).getKey().equals(id)){
                return sysUserRole.getUserId();
            }
        }
        return null;
    }

    @Override
    public String selectLocalJituancaiwuId(String userId) {
        //根据用户ID找到部门ID
        List<DepartIdModel> departIdModels = userDepartService.queryDepartIdsOfUser(userId);
        //这是此用户的部门ID
        String departId = departIdModels.get(0).getKey();
        //根据部门ID找到部门编码
        QueryWrapper<SysDepart> departQueryWrapper = new QueryWrapper<>();
        departQueryWrapper.eq("id",departId);
        List<SysDepart> departList = departService.list(departQueryWrapper);
        //部门编码
        String orgCode = departList.get(0).getOrgCode();
        //取部门编码前六位
        if (StrUtil.isEmpty(orgCode)){
            return null;
        }
        //集团编码
        String substring = orgCode.substring(0, 3);
        //根据集团财务编码找到集团财务的部门ID
        QueryWrapper<SysDepart> departQueryWrapper2 = new QueryWrapper<>();
        departQueryWrapper2.eq("org_code",substring);
        //找到集团财务的对象
        List<SysDepart> departList1 = departService.list(departQueryWrapper2);
        //得到集团财务的ID值
        String id = departList1.get(0).getId();
        //根据ID值筛选角色列表
        //根据角色编码 获取到系统角色对象
        SysRole wuyejingli = sysRoleService.getRoleNoTenant("jituancaiwu");
        //获取到系统角色的id值
        String wuyejingliId = wuyejingli.getId();
        //根据系统角色ID在用户和角色表中找到所有用户的角色是该角色的ID
        QueryWrapper<SysUserRole> sysUserRoleQueryWrapper = new QueryWrapper<>();
        sysUserRoleQueryWrapper.eq("role_id",wuyejingliId);
        //得到用户和系统角色集合都是集团财务的集合 里面包含用户ID 和角色ID
        List<SysUserRole> list = sysUserRoleService.list(sysUserRoleQueryWrapper);
        //遍历此集合找到每个用户
        for (SysUserRole sysUserRole : list) {
            //根据用户ID找到部门ID
            List<DepartIdModel> departIdModels2 = userDepartService.queryDepartIdsOfUser(sysUserRole.getUserId());
            //当集合中的用户的 部门ID和登录用户的部门ID相等时，则返回集合用户的ID值
            if (departIdModels2.get(0).getKey().equals(id)){
                return sysUserRole.getUserId();
            }
        }
        return null;
    }

    @Override
    public String selectLocalJituankuaijiId(String userId) {
        //根据用户ID找到部门ID
        List<DepartIdModel> departIdModels = userDepartService.queryDepartIdsOfUser(userId);
        //这是此用户的部门ID
        String departId = departIdModels.get(0).getKey();
        //根据ID值筛选角色列表
        //根据角色编码 获取到系统角色对象
        SysRole wuyejingli = sysRoleService.getRoleNoTenant("jituankuaiji");
        //获取到系统角色的id值
        String wuyejingliId = wuyejingli.getId();
        //根据系统角色ID在用户和角色表中找到所有用户的角色是该角色的ID
        QueryWrapper<SysUserRole> sysUserRoleQueryWrapper = new QueryWrapper<>();
        sysUserRoleQueryWrapper.eq("role_id",wuyejingliId);
        //得到用户和系统角色集合都是集团财务的集合 里面包含用户ID 和角色ID
        List<SysUserRole> list = sysUserRoleService.list(sysUserRoleQueryWrapper);
        //遍历此集合找到每个用户
        for (SysUserRole sysUserRole : list) {
            //根据用户ID找到部门ID
            List<DepartIdModel> departIdModels2 = userDepartService.queryDepartIdsOfUser(sysUserRole.getUserId());
            //当集合中的用户的 部门ID和登录用户的部门ID相等时，则返回集合用户的ID值
            if (departIdModels2.get(0).getKey().equals(departId)){
                return sysUserRole.getUserId();
            }
        }
        return null;
    }

    @Override
    public String selectLocalWuyezongjingliId(String userId) {
        //根据用户ID找到部门ID
        List<DepartIdModel> departIdModels = userDepartService.queryDepartIdsOfUser(userId);
        //这是此用户的部门ID
        String departId = departIdModels.get(0).getKey();
        //根据部门ID找到部门编码
        QueryWrapper<SysDepart> departQueryWrapper = new QueryWrapper<>();
        departQueryWrapper.eq("id",departId);
        List<SysDepart> departList = departService.list(departQueryWrapper);
        //部门编码
        String orgCode = departList.get(0).getOrgCode();
        //取部门编码前六位
        if (StrUtil.isEmpty(orgCode)){
            return null;
        }
        //集团编码
        String substring = orgCode.substring(0, 3);
        //根据集团财务编码找到集团财务的部门ID
        QueryWrapper<SysDepart> departQueryWrapper2 = new QueryWrapper<>();
        departQueryWrapper2.eq("org_code",substring);
        //找到集团财务的对象
        List<SysDepart> departList1 = departService.list(departQueryWrapper2);
        //得到集团财务的ID值
        String id = departList1.get(0).getId();
        //根据ID值筛选角色列表
        //根据角色编码 获取到系统角色对象
        SysRole wuyejingli = sysRoleService.getRoleNoTenant("wuyezongjingli");
        //获取到系统角色的id值
        String wuyejingliId = wuyejingli.getId();
        //根据系统角色ID在用户和角色表中找到所有用户的角色是该角色的ID
        QueryWrapper<SysUserRole> sysUserRoleQueryWrapper = new QueryWrapper<>();
        sysUserRoleQueryWrapper.eq("role_id",wuyejingliId);
        //得到用户和系统角色集合都是集团财务的集合 里面包含用户ID 和角色ID
        List<SysUserRole> list = sysUserRoleService.list(sysUserRoleQueryWrapper);
        //遍历此集合找到每个用户
        for (SysUserRole sysUserRole : list) {
            //根据用户ID找到部门ID
            List<DepartIdModel> departIdModels2 = userDepartService.queryDepartIdsOfUser(sysUserRole.getUserId());
            //当集合中的用户的 部门ID和登录用户的部门ID相等时，则返回集合用户的ID值
            if (departIdModels2.get(0).getKey().equals(id)){
                return sysUserRole.getUserId();
            }
        }
        return null;
    }

    /**
     * 拉取数据
     * @param monthlySummary
     * @return
     */
    @Override
    public boolean pullShujv(MonthlySummary monthlySummary) {
        //拉取电费
        pullDianfeiItem(monthlySummary);
        //拉取充值费用
        pullChongZhiFree(monthlySummary);
        //拉取物业费应收
//        pullWuyeYingshou(monthlySummary);
        return true;
    }

//    private void pullWuyeYingshou(MonthlySummary monthlySummary) {
//        //全部数据
//        List<GjshReport> gjshReportList = gjshYingshouMapper.selectAll();
//        //本账期和本小区保存的
//        List<GjshReport> localGjshReportList = new ArrayList<>();
//        //筛选出本账期和本社区的
//        for (GjshReport gjshReport : gjshReportList) {
//
//        }
//        //全部表头
//        List<String> columnList = gjshYingshouMapper.selectColumns();
//        //进行 行转列
//        for (GjshReport gjshReport : localGjshReportList) {
//            Yingshou yingshou = new Yingshou();
//            //物业费
//            if (StrUtil.isNotEmpty(gjshReport.getGjshFeeTypeId())&&gjshReport.getGjshFeeTypeId().equals("物业费6折")){
//                yingshou.setPipelinePressurizationfee(gjshReport.getAmount());
//            }
//            //物业费六折
//            if (StrUtil.isNotEmpty(gjshReport.getGjshFeeTypeId())&&gjshReport.getGjshFeeTypeId().equals("物业费")){
//
//            }
//            //商铺物业费
//            if (StrUtil.isNotEmpty(gjshReport.getGjshFeeTypeId())&&gjshReport.getGjshFeeTypeId().equals("商铺物业费")){
//
//            }
//            //卫生费
//            if (StrUtil.isNotEmpty(gjshReport.getGjshFeeTypeId())&&gjshReport.getGjshFeeTypeId().equals("卫生费")){
//
//            }
//            //电费公摊
//            if (StrUtil.isNotEmpty(gjshReport.getGjshFeeTypeId())&&gjshReport.getGjshFeeTypeId().equals("电费公摊")){
//
//            }
//            //水费
//            if (StrUtil.isNotEmpty(gjshReport.getGjshFeeTypeId())&&gjshReport.getGjshFeeTypeId().equals("水费")){
//
//            }
//
//        }
//
//    }

    @Override
    public boolean computedShujv(MonthlySummary monthlySummary) {
        computedZxbzjItem(monthlySummary);
        computedChongZhiFree(monthlySummary);
        computedDianfeiItem(monthlySummary);
        return true;
    }

    /**
     * 拉取充值费用
     * @param monthlySummary
     */
    private void pullChongZhiFree(MonthlySummary monthlySummary) {
        //获取社区名称
        QueryWrapper<SysDepart> departQueryWrapper = new QueryWrapper<>();
        departQueryWrapper.eq("id",monthlySummary.getCommunityId());
        List<SysDepart> departList = departService.list(departQueryWrapper);
        String departName = departList.get(0).getDepartName();
        //获取账期的起始日期
        Date startData = monthlySummary.getStartData();
        Date endData = monthlySummary.getEndData();
        List<OtherPreRecharge> otherPreRecharges = otherPreRechargeMapper.pullOtherPreRecharge();
        //判断只保存 本小区本账期的
        //保存容器
        List<PreRecharge> preRechargeList = new ArrayList<>();
        for (OtherPreRecharge otherPreRecharge : otherPreRecharges) {
            PreRecharge preRecharge = new PreRecharge();
            //筛选不是本小区的
            if (!otherPreRecharge.getCommunityId().equals(departName)){
                continue;
            }
            //筛选不是本账期的
            // -1
            int startCompare = DateUtil.compare(startData, otherPreRecharge.getPaymentTime());
            //1
            int endCompare = DateUtil.compare(endData, otherPreRecharge.getPaymentTime());
            if (!((startCompare <= 0 )&&(endCompare >= 0))){
                continue;
            }
            //设置账期ID
            preRecharge.setZhangId(monthlySummary.getId());
            //设置账期月份
            preRecharge.setZhangMonth(monthlySummary.getMonth());
            //设置社区ID
            preRecharge.setCommunityName(monthlySummary.getCommunityId());
            //设置VALUE值
            preRecharge.setMoney(otherPreRecharge.getMoney());
            //设置支付方式
            if (StrUtil.isEmpty(otherPreRecharge.getMark())){
                //如果备注为空 则是线下支付
                String qianValue = "线下-"+otherPreRecharge.getPaymentMethod();
                QueryWrapper<SysCategory> categoryQueryWrapper = new QueryWrapper<>();
                categoryQueryWrapper.eq("name",qianValue);
                List<SysCategory> categoryList = sysCategoryService.list(categoryQueryWrapper);
                //设置支付方式ID
                preRecharge.setPaymentMethod(categoryList.get(0).getId());
            }else {
                //线上支付
                String qianValue = "线下-"+otherPreRecharge.getPaymentMethod();
                QueryWrapper<SysCategory> categoryQueryWrapper = new QueryWrapper<>();
                categoryQueryWrapper.eq("name",qianValue);
                List<SysCategory> categoryList = sysCategoryService.list(categoryQueryWrapper);
                //设置支付方式ID
                preRecharge.setPaymentMethod(categoryList.get(0).getId());
            }
            //设置类型TYPE
            preRecharge.setType(otherPreRecharge.getType());
            //设置备注
            preRecharge.setMark(otherPreRecharge.getMark());
            //设置缴费时间
            preRecharge.setPaymentTime(otherPreRecharge.getPaymentTime());
            //设置业主姓名
            preRecharge.setOwnerName(otherPreRecharge.getOwnerName());
            //设置房产编号
            preRecharge.setPropertyNumber(otherPreRecharge.getPropertyNumber());
            //添加到容器内
            preRechargeList.add(preRecharge);
        }
        //保存数据
        preRechargeService.saveBatch(preRechargeList);
    }
    /**
     * 计算充值费用
     * @param monthlySummary
     */
    private void computedChongZhiFree(MonthlySummary monthlySummary) {
        QueryWrapper<PreRecharge> preRechargeQueryWrapper = new QueryWrapper<>();
        preRechargeQueryWrapper.eq("zhang_id",monthlySummary.getId());
        List<PreRecharge> preRechargeList = preRechargeService.list(preRechargeQueryWrapper);
        //计算充值费用
        MonthlySummaryItem monthlySummaryItem = new MonthlySummaryItem();
        //判断是否有数据
        if (CollectionUtil.isEmpty(preRechargeList)){
            //为空的情况 直接初始化
            //设置上期余额
            monthlySummaryItem.setOpeningBalance(BigDecimal.ZERO);
            //设置本期收入数
            monthlySummaryItem.setCurrentPeriodReceivable(BigDecimal.ZERO);
            //设置本期付出款
            monthlySummaryItem.setCurrentPeriodReceived(BigDecimal.ZERO);
            //设置余额
            monthlySummaryItem.setEndBalance(BigDecimal.ZERO);
            //设置已收构成
            //现金
            monthlySummaryItem.setReceivedXj(BigDecimal.ZERO);
            //App线上支付
            monthlySummaryItem.setReceivedAppxszf(BigDecimal.ZERO);
            //扫码支付
            monthlySummaryItem.setReceivedSmzf(BigDecimal.ZERO);
            //电汇
            monthlySummaryItem.setReceivedDh(BigDecimal.ZERO);
            //预售划款
            monthlySummaryItem.setReceivedYshk(BigDecimal.ZERO);
            //其他
            monthlySummaryItem.setReceivedQt(BigDecimal.ZERO);
        }else{
            //计算费用
            //设置上期余额
            BigDecimal openingBalance = BigDecimal.ZERO;
            //设置本期收入数
            BigDecimal currentPeriodReceivable = BigDecimal.ZERO;
            //本期付出数
            BigDecimal currentPeriodReceived = BigDecimal.ZERO;
            //设置余额
            BigDecimal endBalance = BigDecimal.ZERO;
            //设置收入明细
            //现金累加
            BigDecimal receivedXj = BigDecimal.ZERO;
            //扫码支付累加
            BigDecimal receivedSmzf = BigDecimal.ZERO;
            //线上支付累加
            BigDecimal receivedAppxszf = BigDecimal.ZERO;
            //电汇累加
            BigDecimal receivedDh = BigDecimal.ZERO;
            //预售划款累加
            BigDecimal receivedYshk = BigDecimal.ZERO;
            //其他累加
            BigDecimal receivedQt = BigDecimal.ZERO;
            //不为空的情况
            for (PreRecharge preRecharge : preRechargeList) {
                //查出分类字典的code值
                QueryWrapper<SysCategory> categoryQueryWrapper = new QueryWrapper<>();
                categoryQueryWrapper.eq("id",preRecharge.getPaymentMethod());
                List<SysCategory> categoryList = sysCategoryService.list(categoryQueryWrapper);
                String code = "";
                String codeValue = "";
                if (CollectionUtil.isNotEmpty(categoryList)){
                    //获取code的前两类
                    code = categoryList.get(0).getCode().substring(0,6);
                    //获取code值
                    codeValue = categoryList.get(0).getCode();
                }
                BigDecimal money = preRecharge.getMoney();
                if (money == null) {
                    // 设置一个默认值，如0
                    money = BigDecimal.ZERO;
                }
                //本期收入款和付出款的累加
                if (money.compareTo(BigDecimal.ZERO) >= 0){
                    //计算收入款
                    currentPeriodReceivable = currentPeriodReceivable.add(money);
                    //计算 收支明细的数据
                    //现金累加
                    if (codeValue.equals("C03A02A01")){
                        receivedXj = receivedXj.add(money);
                    }
                    //APP线上支付的累加
                    if (code.equals("C03A01")){
                        receivedAppxszf = receivedAppxszf.add(money);
                    }
                    //扫码支付
                    if (code.equals("C03A02")){
                        receivedAppxszf = receivedAppxszf.add(money);
                    }
                    //电汇
                    //预存冲抵
                    //其他
                }else {
                    currentPeriodReceived.add(money);
                }
            }
            //设置 收支明细
            //设置上期余额
            monthlySummaryItem.setOpeningBalance(openingBalance);
            //设置本期收入款
            monthlySummaryItem.setCurrentPeriodReceivable(currentPeriodReceivable);
            //设置本期付出款
            monthlySummaryItem.setCurrentPeriodReceived(currentPeriodReceived.abs());
            //设置余额
            monthlySummaryItem.setEndBalance(currentPeriodReceivable.subtract(currentPeriodReceived.abs()));
            //设置收入构成
            //现金支付
            monthlySummaryItem.setReceivedXj(receivedXj);
            //线上支付
            monthlySummaryItem.setReceivedAppxszf(receivedAppxszf);
            //扫码支付
            monthlySummaryItem.setReceivedSmzf(receivedSmzf);
            //电汇
            monthlySummaryItem.setReceivedDh(receivedDh);
            //预售划款
            monthlySummaryItem.setReceivedYshk(receivedYshk);
            //其他
            monthlySummaryItem.setReceivedQt(receivedQt);
        }
        //设置项目ID
        monthlySummaryItem.setAccountPeriodId(monthlySummary.getId());
        //设置项目
        monthlySummaryItem.setReportItem("预存物业费");
        //设置项目分组
        monthlySummaryItem.setReportItemGroup("其他应付款");
        //设置展示顺序
        monthlySummaryItem.setShowOrder(14);
        //保存报表
        monthlySummaryItemService.save(monthlySummaryItem);
    }



    /**
     * 创建汇总表记录的服务
     */
    @Autowired
    private ISysUserDepartService userDepartService;
    private String getLoginUserDeptId(String id) {
        List<DepartIdModel> departIdModels = userDepartService.queryDepartIdsOfUser(id);
        if (CollectionUtil.isNotEmpty(departIdModels)) {
            return departIdModels.get(0).getValue();
        }
        return null;
    }
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IHywyReportProcessApproveRecordService hywyReportProcessApproveRecordService;
    @Override
    public void createMonthlySummaryValue() {
        //获取固定用户
        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.eq("realname","李四");
        List<SysUser> sysUserList = sysUserService.list(sysUserQueryWrapper);
        SysUser sysUser = sysUserList.get(0);
        //获取当前年份和月份
        // 获取当前日期
        Date now = new Date();
        // 设置为当年当月的 21 号 0 点 0 分 0 秒
        // 设置为当月的第一天
        Date targetDate = DateUtil.beginOfMonth(now);
        // 增加 20 天，即设置为 21 号
        targetDate = DateUtil.offsetDay(targetDate, 20);
        // 设置 00 00 00
        targetDate = DateUtil.beginOfDay(targetDate);
        // 增加 20 天，即设置为 21 号
        targetDate = DateUtil.offsetMonth(targetDate, -1);
        // 格式化年份和月份  2024-04
        String yearAndMonth = DateUtil.format(targetDate, "yyyy-MM");
        // 获取下个月的今天
        Date nextMonth = DateUtil.offsetMonth(targetDate, 1);
        // 使用 Calendar 设置为下个月的20号
        Calendar calendar = DateUtil.calendar(nextMonth);
        calendar.set(Calendar.DAY_OF_MONTH, 20);
        // 获取下个月20号的Date对象
        Date nextMonthTwentieth = calendar.getTime();
        // 设置时间为23:59:59
        //下月 20号23:59:59
        Date endOfDay = DateUtil.endOfDay(nextMonthTwentieth);
        // 转成字符串再 转为日期对象
        String endOfDayString = DateUtil.formatDateTime(endOfDay);
        DateTime parse = DateUtil.parse(endOfDayString);
        //获取部门集合
        QueryWrapper<SysDepart> departQueryWrapper = new QueryWrapper<>();
        departQueryWrapper.eq("org_type",3);
        List<SysDepart> sysDepartList = departService.list(departQueryWrapper);
        //获取年月拼接数组
        String[] split = yearAndMonth.split("-");
        //遍历部门集合 并创建记录
        for (SysDepart sysDepart : sysDepartList) {
            int i = Integer.parseInt(split[1]);
            String name = sysDepart.getDepartName()+"小区"+split[0]+"年"+i+"月份收入报表";
            //创建汇总数据对象
            MonthlySummary monthlySummary = new MonthlySummary();
            //设置报表名称
            monthlySummary.setReportName(name);
            //设置月份
            monthlySummary.setMonth(yearAndMonth);
            //设置社区ID
            monthlySummary.setCommunityId(sysDepart.getId());
            //设置进度
            monthlySummary.setProgress("0");
            //设置起止日期
            monthlySummary.setStartData(targetDate);
            monthlySummary.setEndData(parse);
            //设置创建人
            monthlySummary.setCreateBy(sysUser.getUsername());
            //设置部门code
            monthlySummary.setSysOrgCode(sysDepart.getOrgCode());
            //保存汇总表的记录
            save(monthlySummary);
            // 获取当前数据
            QueryWrapper<MonthlySummary> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("report_name", name);
            //创建审批记录
            List<MonthlySummary> monthlySummaryList = list(queryWrapper);
            //获取汇总表的ID值
            String id = monthlySummaryList.get(0).getId();
            //获取汇总的创建人
            String createBy = monthlySummaryList.get(0).getCreateBy();
            QueryWrapper<SysUser> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("username",createBy);
            //获取创建人的对象
            List<SysUser> sysUserList1 = sysUserService.list(queryWrapper2);
            String userId = sysUserList1.get(0).getId();
            HywyReportProcessApproveRecord hywyReportProcessApproveRecord = new HywyReportProcessApproveRecord();
            //设置报表id
            hywyReportProcessApproveRecord.setBusinessKey(id);
            //设置审批人
            hywyReportProcessApproveRecord.setApprover(userId);
            //设置任务名称
            hywyReportProcessApproveRecord.setTaskName("物业专员新建");
            //设置审批结果
            hywyReportProcessApproveRecord.setApproveResult("Y");

            hywyReportProcessApproveRecordService.save(hywyReportProcessApproveRecord);
        }




    }

    /**
     * 拉取电费
     * @param monthlySummary
     */
    private void pullDianfeiItem(MonthlySummary monthlySummary){
        //获取社区名称
        QueryWrapper<SysDepart> departQueryWrapper = new QueryWrapper<>();
        departQueryWrapper.eq("id",monthlySummary.getCommunityId());
        List<SysDepart> departList = departService.list(departQueryWrapper);
        String departName = departList.get(0).getDepartName();
        QueryWrapper<OtherElectricCharge> electricChargeQueryWrapper = new QueryWrapper<>();
        //获取账期
        Date startData = monthlySummary.getStartData();
        Date endData = monthlySummary.getEndData();
        electricChargeQueryWrapper
                .eq("community_name",departName)
                .between("sale_date",startData,endData);
//        List<ElectricCharge> electricChargeList = electricChargeService.list(electricChargeQueryWrapper);
        //另外数据源拉取
        List<OtherElectricCharge> otherElectricChargeList = otherElectricChargeMapper.selectList(electricChargeQueryWrapper);
        //保存数据
        List<ElectricCharge> electricChargeList = new ArrayList<>();
        for (OtherElectricCharge electricCharge : otherElectricChargeList) {
            ElectricCharge electricCharge1 = new ElectricCharge();
            //设置社区ID
            electricCharge1.setCommunityName(monthlySummary.getCommunityId());
            //设置用户名
            electricCharge1.setOwnerNumber(electricCharge.getOwnerNumber());
            //设置购买数量
            electricCharge1.setPurchaseQuantity(electricCharge.getPurchaseQuantity());
            //设置购买金额
            electricCharge1.setPurchaseAmount(electricCharge.getPurchaseAmount());
            //设置支付方式
            QueryWrapper<SysCategory> categoryQueryWrapper = new QueryWrapper<>();
            categoryQueryWrapper.eq("name",electricCharge.getPaymentWay());
            List<SysCategory> categoryList = sysCategoryService.list(categoryQueryWrapper);
            electricCharge1.setPaymentWay(categoryList.get(0).getId());
            //设置缴费时间
            electricCharge1.setSaleDate(electricCharge.getSaleDate());
            //设置创建时间
            electricCharge1.setCreateTime(electricCharge.getCreateTime());
            electricCharge1.setCreateType("1");
            //设置账期ID
            electricCharge1.setZhangId(monthlySummary.getId());
            //设置账期月份
            electricCharge1.setZhangMonth(monthlySummary.getMonth());
            //添加到容器
            electricChargeList.add(electricCharge1);
        }
        electricChargeService.saveBatch(electricChargeList);
    }

    /**
     * 计算电费
     * @param monthlySummary
     */
    private void computedDianfeiItem(MonthlySummary monthlySummary){
        //获取该账期下的所有电费数据
        QueryWrapper<ElectricCharge> electricChargeQueryWrapper = new QueryWrapper<>();
        electricChargeQueryWrapper.eq("zhang_id",monthlySummary.getId());
        List<ElectricCharge> electricChargeList = electricChargeService.list(electricChargeQueryWrapper);
        MonthlySummaryItem monthlySummaryItem = new MonthlySummaryItem();
        if (CollectionUtil.isEmpty(electricChargeList)){
            //设置本期应收
            monthlySummaryItem.setCurrentPeriodReceivable(BigDecimal.ZERO);
            //设置本期已收数
            monthlySummaryItem.setCurrentPeriodReceived(BigDecimal.ZERO);
            //为已收构成赋初始值
            monthlySummaryItem.setReceivedXj(BigDecimal.ZERO);
            monthlySummaryItem.setReceivedAppxszf(BigDecimal.ZERO);
            monthlySummaryItem.setReceivedSmzf(BigDecimal.ZERO);
            monthlySummaryItem.setReceivedDh(BigDecimal.ZERO);
            monthlySummaryItem.setReceivedYshk(BigDecimal.ZERO);
            monthlySummaryItem.setReceivedQt(BigDecimal.ZERO);
        }else {
            //本期应收数
            BigDecimal currentPeriodReceived = BigDecimal.ZERO;
            //现金累加
            BigDecimal receivedXj = BigDecimal.ZERO;
            //扫码支付累加
            BigDecimal receivedSmzf = BigDecimal.ZERO;
            //线上支付累加
            BigDecimal receivedAppxszf = BigDecimal.ZERO;
            //电汇累加
            BigDecimal receivedDh = BigDecimal.ZERO;
            //预售划款累加
            BigDecimal receivedYshk = BigDecimal.ZERO;
            //其他累加
            BigDecimal receivedQt = BigDecimal.ZERO;
            for (ElectricCharge electricCharge : electricChargeList) {
                QueryWrapper<SysCategory> categoryQueryWrapper = new QueryWrapper<>();
                categoryQueryWrapper.eq("id",electricCharge.getPaymentWay());
                List<SysCategory> categoryList = sysCategoryService.list(categoryQueryWrapper);
                //设置缴费方式
                String code = "";
                String codeValue = "";
                if (categoryList!= null){
                    code = categoryList.get(0).getCode().substring(0,6);
                    codeValue = categoryList.get(0).getCode();
                }
                BigDecimal purchaseAmount = electricCharge.getPurchaseAmount();
                if (purchaseAmount != null){
                    //累加应收数
                    currentPeriodReceived = currentPeriodReceived.add(purchaseAmount);
                }
                //设置已收构成
                //现金累加
                if (codeValue.equals("C03A02A01")){
                    receivedXj = receivedXj.add(electricCharge.getPurchaseAmount());
                }
                //线上支付累加
                if (code.equals("C03A01")){
                    receivedAppxszf = receivedAppxszf.add(electricCharge.getPurchaseAmount());
                }
                //扫码支付
                if (code.equals("C03A02")){
                    receivedSmzf = receivedSmzf.add(electricCharge.getPurchaseAmount());
                }
            }
            //设置应收数
            monthlySummaryItem.setCurrentPeriodReceived(currentPeriodReceived);
            //设置实收数
            monthlySummaryItem.setCurrentPeriodReceivable(currentPeriodReceived);
            //设置收入构成
            //现金支付
            monthlySummaryItem.setReceivedXj(receivedXj);
            //线上支付
            monthlySummaryItem.setReceivedAppxszf(receivedAppxszf);
            //扫码支付
            monthlySummaryItem.setReceivedSmzf(receivedSmzf);
            //电汇
            monthlySummaryItem.setReceivedDh(receivedDh);
            //预售划款
            monthlySummaryItem.setReceivedYshk(receivedYshk);
            //其他
            monthlySummaryItem.setReceivedQt(receivedQt);

        }
        //设置期初未收款
        monthlySummaryItem.setOpeningBalance(BigDecimal.ZERO);
        //设置期末收款
        monthlySummaryItem.setEndBalance(BigDecimal.ZERO);
        //设置项目ID
        monthlySummaryItem.setAccountPeriodId(monthlySummary.getId());
        //设置项目
        monthlySummaryItem.setReportItem("电费");
        //设置项目分组
        monthlySummaryItem.setReportItemGroup("其他收入");
        //设置展示顺序
        monthlySummaryItem.setShowOrder(12);
        //保存报表
        monthlySummaryItemService.save(monthlySummaryItem);
    }

    /**
     * 计算装修保证金
     * @param monthlySummary
     */
    private void computedZxbzjItem(MonthlySummary monthlySummary){
        //获取账期
        Date startData = monthlySummary.getStartData();
        Date endData = monthlySummary.getEndData();

        //获取上期账期
        // 创建 Calendar 实例并将原始日期设置到 Calendar 对象中
        Calendar startCal = Calendar.getInstance();
        startCal.setTime(startData);

        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endData);

        // 将月份减1
        startCal.add(Calendar.MONTH, -1);
        endCal.add(Calendar.MONTH, -1);

        // 将 Calendar 对象转换回 Date 对象
        Date UpstartData = startCal.getTime();
        Date UpendData = endCal.getTime();

        //获取装修保证金的上个账期的所有 当前小区 且是上一个账期的所有数据
        QueryWrapper<DecorationDeposit> queryWrapper= new QueryWrapper<>();
//        queryWrapper.eq("community_name",monthlySummary.getCommunityId()).ge("collection_date", UpstartData)
//                .le("collection_date", UpendData);
        queryWrapper.eq("community_name",monthlySummary.getCommunityId())
                .between("collection_date",UpstartData,UpendData);
        List<DecorationDeposit> lastDecorationDepositList = decorationDepositService.list(queryWrapper);
        MonthlySummaryItem monthlySummaryItem = new MonthlySummaryItem();

        //获取装修保证金的这个账期的所有 当前小区 且是此账期的所有数据
        QueryWrapper<DecorationDeposit> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("community_name",monthlySummary.getCommunityId())
                .ge("collection_date", startData)
                .le("collection_date", endData);
        List<DecorationDeposit> nowDecorationDepositList = decorationDepositService.list(queryWrapper2);

        //计算上期结存数并拉入
        if (lastDecorationDepositList.isEmpty()){
            //设置上期结存数默认值
            monthlySummaryItem.setOpeningBalance(BigDecimal.ZERO);
        }else {
            //累加上期结存数
            BigDecimal openingBalance1 = BigDecimal.ZERO;
            BigDecimal openingBalance2 = BigDecimal.ZERO;
            //计算上期结存数 累加
            for (DecorationDeposit decorationDeposit : lastDecorationDepositList) {
                Double depositCollection = decorationDeposit.getDepositCollection();
                Double tuibaozhengjin = decorationDeposit.getTuibaozhengjin();
                if (depositCollection != null) {
                    openingBalance1 = openingBalance1.add(BigDecimal.valueOf(depositCollection));
                }
                if (tuibaozhengjin != null) {
                    openingBalance2 = openingBalance2.add(BigDecimal.valueOf(tuibaozhengjin));
                }
            }
            //设置上期结存数
            monthlySummaryItem.setOpeningBalance(openingBalance1.subtract(openingBalance2));
        }
        //计算本期收入款
        //计算本期付出款
        if (nowDecorationDepositList.isEmpty()){
            //设置本期收入款默认值
            monthlySummaryItem.setCurrentPeriodReceivable(BigDecimal.ZERO);
            //设置本期付出款 默认值
            monthlySummaryItem.setCurrentPeriodReceived(BigDecimal.ZERO);
            //设置余额默认值
            monthlySummaryItem.setEndBalance(BigDecimal.ZERO);
        }else {
            //累加本期收入款
            BigDecimal currentPeriodReceivable = BigDecimal.ZERO;
            //累加本期付出款
            BigDecimal currentPeriodReceived = BigDecimal.ZERO;
            for (DecorationDeposit decorationDeposit : nowDecorationDepositList) {
                Double tuibaozhengjin = decorationDeposit.getTuibaozhengjin();
                Double depositCollection = decorationDeposit.getDepositCollection();
                if (depositCollection != null) {
                    currentPeriodReceivable = currentPeriodReceivable.add(BigDecimal.valueOf(depositCollection));
                }
                if (tuibaozhengjin != null) {
                    currentPeriodReceived = currentPeriodReceived.add(BigDecimal.valueOf(tuibaozhengjin));
                }
            }
            //赋值
            monthlySummaryItem.setCurrentPeriodReceivable(currentPeriodReceivable);
            monthlySummaryItem.setCurrentPeriodReceived(currentPeriodReceived);
        }

        //计算余额
        BigDecimal endBalance = BigDecimal.ZERO;
        endBalance = endBalance.add(monthlySummaryItem.getCurrentPeriodReceivable())
                .add(monthlySummaryItem.getOpeningBalance())
                .subtract(monthlySummaryItem.getCurrentPeriodReceived());
        monthlySummaryItem.setEndBalance(endBalance);

        //设置账期id
        monthlySummaryItem.setAccountPeriodId(monthlySummary.getId());
        //设置项目
        monthlySummaryItem.setReportItem("装修保证金");
        //设置项目分组
        monthlySummaryItem.setReportItemGroup("其他应付款");
        //设置展示顺序
        monthlySummaryItem.setShowOrder(13);
        monthlySummaryItemService.save(monthlySummaryItem);

    }
}
